import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
// 导入组件
import login from '@/views/login/index.vue'
import layout from '@/views/layout/index.vue'
import dashboard from '@/views/dashboard/index.vue'
import article from '@/views/article/index.vue'
Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/layout' },
  { path: '/login', component: login },
  {
    path: '/layout',
    component: layout,
    children: [
      { path: '/', redirect: '/dashboard' },
      { path: '/dashboard', component: dashboard },
      { path: '/article', component: article }
    ]
  }
]

const router = new VueRouter({
  routes
})
// 前置全局路由导航守卫
router.beforeEach((to, from, next) => {
  /* 登录权限：有权限就正常跳转，没有权限就跳转登录页
     可以跳转：：有token川跳转登录页 */
  if (store.state.user.token || to.path === '/login') {
    next()
  } else {
    next('/login')
  }
})
export default router
